<?php
/**
 * 推荐有奖
 *
 *
 **/

defined('InShopNC') or exit('Access Invalid!');
class share_recommendControl extends SystemControl{
    //const EXPORT_SIZE = 5000;
    public function __construct() {
        parent::__construct ();
        //Language::read('goods');
    }
    
    public function indexOp(){
        $model_activity = Model('lottery_activity');
        $model_gift = Model('lottery_gift');
        if($_POST['form_submit'] !='ok'){
            //只允许，开始时间结束时间和图片，规则
            //查找推荐有奖活动
            $condition = array();
            $condition['lottery_activity_id']=999;
            $activity = $model_activity->getActivityInfo($condition);
            //查找推荐有奖活动礼品
            $gift_con['lottery_gift_id']=999;
            $gift = $model_gift->getGiftInfo($condition);
            
            //查找对应代金券模板列表
            $model_voucher=Model('voucher');
            $voucher_con['voucher_t_state']=1;
            $voucher_list=$model_voucher->getVoucherTemplateList($voucher_con);
            
            Tpl::output('voucher_list',$voucher_list);
            Tpl::output('activity',$activity);
            Tpl::output('gift',$gift);
            Tpl::showpage('share_recommend.edit');
        }else{
            $data['exchange_end_date']=strtotime(trim($_POST['exchange_end_date']));
            $data['exchange_start_date']=strtotime(trim($_POST['exchange_start_date']));
            
            $data['comments']=htmlspecialchars_decode($_POST['comments']);
            //$data['comments']=($_POST['comments']);
            if ($data['exchange_end_date']<$data['exchange_start_date']){
                showDialog("结束时间必须大于开始时间!");
            }
            $upload		= new UploadFile();
            if($_FILES['image']['name'] != '' ){
            
                $upload->set('default_dir',ATTACH_ACTIVITY);
                //读取UPLOAD_SITE_URL.'/'.ATTACH_ACTIVITY.'/'.$info['image'];
                $result = $upload->upfile('image');
                if (!$result){
                    showMessage($upload->error,'','','error');
                }
                $data['image'] = $upload->file_name;
            
            }
            
            //保存
            $result = $model_activity->editActivityById($data,999);
            if($result) {
                //修改活动礼品对应领取的代金券
                if($_POST['goods_id']!=null && !empty($_POST['goods_id'])){
                    $update['goods_id']=$_POST['goods_id'];
                    $gift_id=999;
                    $model_gift->updateGift($update,$gift_id);
                }
                
                
                showDialog("抽奖活动修改成功!");
            } else {
                showDialog("保存失败!");
                return;
            }
        }
        
    }
}
    